import Sprite from './base/sprite'
import DataBus from './databus'

const RECT_IMG_SRC = 'images/rect_2.png'

let databus = new DataBus()
let start_x = databus.start_x
let line_num = databus.line_num

export default class DItem extends Sprite {
  constructor(imgSrc = RECT_IMG_SRC,x,y,w,h,l) {
    super(imgSrc, x, y, w, h)
    
    this.location = l //默认值，不切换 - 其他如九宫格切换 5;1,3,9,7;2,6,8,4;

  }
  //复用是重置属性值
  reInit(imgSrc,x,y,w,h,l){
    this.img.src = imgSrc
    this.x = x
    this.y = y
    this.w = w
    this.h = h
    this.location = l
  }

  willTurn(x_change){
    //出界判断
    if ((this.x + x_change < start_x) || (this.x + x_change >= line_num)) {
      return false;
    }
    
    let will_down_y = Math.floor(this.y + 1);//向下移动最终位置
    let isLoacation2 = databus.isCanLocationItem([this.x + x_change, will_down_y, this.w, this.h]);
    if (isLoacation2) {
      // let down_y = Math.floor(this.y);//当前上距的位置
      // let isLoacation1 = databus.isCanLocationItem([this.x + x_change, down_y, this.w, this.h]);
      // if (isLoacation1) {//有足够的位置，平移即可
      //   return 1
      // } else {//并向下移动一位置
      return true //去除平移和下移逻辑，直接平移，减少计算 - 更改为直接平移，2020.3.7，livesxu
      // }
    } else {
      return false
    }
  }

  didTurn(x_change,flag){
    // if(flag === 1){
      this.x += x_change
    // } else {
    //   let will_down_y = Math.floor(this.y + 1);//向下移动最终位置
    //   this.x += x_change
    //   this.y = will_down_y
    // }
  }

  willDown(e_down){
    //过界判断
    if ((this.y + this.h) >= databus.c_height) {
      return false;
    }

    let will_down_y = Math.floor(this.y +1) + e_down;//向下移动最终位置
    let isLoacation = databus.isCanLocationItem([this.x, will_down_y, this.w, this.h]);
    return isLoacation;
  }

  didDown(){

    this.y += 1 / 10;//向下移动展示位置

    if (databus.propState === 6) //6.下落速度加快
    {
      this.y += 2 / 10;
    }
    //是否碰道具
    if (databus.prop_item.visible) {
      if (Math.round(this.x) === databus.prop_item.x &&
        Math.floor(this.y + 1) === databus.prop_item.y &&
        Math.round(this.w) === databus.prop_item.w &&
        Math.round(this.h) === databus.prop_item.h) {
        databus.propState = databus.prop_item.p //道具状态生效
        databus.prop_item.visible = false       //道具不再展示
        databus.prop_item.time_show = 0         //道具展示时间为0
        databus.prop_item.time_store = 1000      //道具生效时间
        if (databus.propState === 2) //2.计时器
        {
          databus.time_end = Date.now() + 1000 * 60 * 1 //1分钟
          databus.time_in = true
          databus.propState = 0;
        }
        if (databus.propState === 7) //7.钻石
        {
          databus.score += 5
          databus.propState = 0;
        }
        if (databus.propState === 6) //6.下落速度加快 下落道具时间减半
        {
          databus.prop_item.time_store = 500
        }
      }
    }
    
  }

  willTransform(){
    switch(this.location){
      case 0:
        break;
      case 1:
        // this.x += 2;
        // this.location = 3;
        return this.willTurn(2)
        break;
      case 2:
        // this.x += 1;
        // this.y += 1;
        // this.location = 6;
        return (this.willTurn(1) && this.willDown(1))
        break;
      case 3:
        // this.y += 2;
        // this.location = 9;
        return this.willDown(2)
        break;
      case 4:
        // this.x += 1;
        // this.y -= 1;
        // this.location = 2;
        return (this.willTurn(1) && this.willDown(-1))
        break;
      case 5:
        break;
      case 6:
        // this.x -= 1;
        // this.y += 1;
        // this.location = 8;
        return (this.willTurn(-1) && this.willDown(1))
        break;
      case 7:
        // this.y -= 2;
        // this.location = 1;
        return this.willDown(-2)
        break;
      case 8:
        // this.x -= 1;
        // this.y -= 1;
        // this.location = 4;
        return (this.willTurn(-1) && this.willDown(-1))
        break;
      case 9:
        // this.x -= 2;
        // this.location = 7;
        return this.willTurn(-2)
        break;
    }
  }

  didTransform() {
    switch (this.location) {
      case 0:
        break;
      case 1:
        this.x += 2;
        this.location = 3;
        break;
      case 2:
        this.x += 1;
        this.y += 1;
        this.location = 6;
        break;
      case 3:
        this.y += 2;
        this.location = 9;
        break;
      case 4:
        this.x += 1;
        this.y -= 1;
        this.location = 2;
        break;
      case 5:
        break;
      case 6:
        this.x -= 1;
        this.y += 1;
        this.location = 8;
        break;
      case 7:
        this.y -= 2;
        this.location = 1;
        break;
      case 8:
        this.x -= 1;
        this.y -= 1;
        this.location = 4;
        break;
      case 9:
        this.x -= 2;
        this.location = 7;
        break;
    }
  }
}